<table width="100%" border="0" cellpadding="0" cellspacing="0" background="/images/mainpage-sliced_16.jpg">
  <tr>
    <td width="20" height="40">&nbsp;</td>
    <td class="style5" style="border-right:solid 1px #FFFFFF;">Cost Structure (per day)</td>
  </tr>
</table>
<?php if ($tasks->count()==0){
  ?><p class="errormessage"><?php echo "There is no project data." ?></p><?php
}
else {
?>
<table width="100%" style="padding-left:5px">
  <thead>
  <tr class="style2" background="/images/mainpage-sliced_11.jpg">
    <td align="center"><strong>Date</strong></td>
    <td align="center"><strong>Tasks</strong></td>
    <td align="center"><strong>Milestones</strong></td>
    <td align="center"><strong>Involved Resources</strong></td>
    <td align="center"><strong>Total Cost Per Day</strong></td>    
  </tr>
  </thead>
  <?php 
  $costperday = array();
  $activeTasks = array();
  $activeMilestones = array();
  $resourceCount = array();
  $resourceNames =  array();
  $format = "d-M-Y";
  foreach ($tasks as $i => $task):
    $startDate = date($format,strtotime($task['startDate']));
    $checkDate = $startDate;
    $endDate = date($format,strtotime($task['endDate']));
    $resourceCost = $task['resourceCost'];
	$resourceId = $task['resourceId'];
	$resourceNames[$resourceId] = $task['resourceName'];
	$taskId = $task['taskid'];	
	$milestoneId = $task['milestoneid'];	
    while ($checkDate != $endDate) {      
	  if (array_key_exists($checkDate, $costperday))	  
        $costperday[$checkDate] += $resourceCost;
	  else
	    $costperday[$checkDate] = $resourceCost;
		
	  if (array_key_exists($checkDate, $activeTasks)){
	    if (!in_array($taskId, $activeTasks[$checkDate]))	  
     	  $activeTasks[$checkDate][] = $taskId;
	  }        
	  else
	    $activeTasks[$checkDate][] = $taskId;
	  
	  if (array_key_exists($checkDate, $activeMilestones)){
	    if (!in_array($milestoneId, $activeMilestones[$checkDate]))	  
     	  $activeMilestones[$checkDate][] = $milestoneId;
	  }        
	  else
	    $activeMilestones[$checkDate][] = $milestoneId;
		
	  if (array_key_exists($checkDate, $resourceCount)){
	    if (!in_array($resourceId, $resourceCount[$checkDate]))	  
          $resourceCount[$checkDate][] = $resourceId;
	  }
	  else
	    $resourceCount[$checkDate][] = $resourceId;
	   
      $checkDate = date ($format, strtotime ("+1 day", strtotime($checkDate)));      
    }
	if (array_key_exists($checkDate, $costperday))
      $costperday[$checkDate] += $resourceCost;
	else
	  $costperday[$checkDate] = $resourceCost;
	
    if (array_key_exists($checkDate, $activeTasks)){
	  if (!in_array($taskId, $activeTasks[$checkDate]))	  
     	$activeTasks[$checkDate][] = $taskId;
	}	      
	else
      $activeTasks[$checkDate][] = $taskId;
		
	if (array_key_exists($checkDate, $activeMilestones)){
      if (!in_array($milestoneId, $activeMilestones[$checkDate]))	  
        $activeMilestones[$checkDate][] = $milestoneId;
	}        
	else
	  $activeMilestones[$checkDate][] = $milestoneId;
	
	if (array_key_exists($checkDate, $resourceCount)){
	  if (!in_array($resourceId, $resourceCount[$checkDate]))	  
        $resourceCount[$checkDate][]= $resourceId;
	}
	else
	  $resourceCount[$checkDate][]= $resourceId;
	  
  endforeach;
  $i=0;
  foreach ($costperday as $date => $rowItem):
  $i++;?>
  <tr align="center" class="regulartext_<?php echo fmod($i, 2) ? 'even' : 'odd' ?>">	
	<td><?php echo $date; ?></td>
    <td><?php echo count($activeTasks[$date]); ?></td>
    <td><?php echo count($activeMilestones[$date]); ?></td>
    <td>
	<?php 
	  $resourcesInvolved = count($resourceCount[$date]) .'(';
      for ($j = 0; $j < count($resourceCount[$date]); $j++){
	    if ($j > 0)
		  $resourcesInvolved .= ', ';		
		$resourcesInvolved .= '<a style="text-decoration:underline" href="'. url_for('resource/show/'.$resourceCount[$date][$j]).'">'.$resourceNames[$resourceCount[$date][$j]].'</a>';
      }
	  $resourcesInvolved = $resourcesInvolved .')';	  
	  echo $resourcesInvolved; 
	?>
    </td>
	<td><?php echo number_format($rowItem); ?></td>
  </tr>
  <?php endforeach;?>
</table>
<?php } ?>
<hr/>